package com.ybkj.daijia.server.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.config.RentApi;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.content.Content;
import com.ybkj.daijia.server.model.content.Property;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Blazk.
 */
@RestController
@RequestMapping(value = "rentcontent")
public class RentContentController {

    @Autowired
    private RentApi rentApi;
    @Autowired
    private SettingUtils settingUtils;

    @RequestMapping(value = "contentlist", method = RequestMethod.GET)
    public ModelAndView contentlist() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("rentproperty")) {

            ModelAndView mv = new ModelAndView();
            mv.setViewName("rentproperty/property");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @ResponseBody
    @RequestMapping(value = "insertContent", method = RequestMethod.POST)
    public Map<String, Object> insertContent(
        @RequestParam(required = false, value = "contentName") String contentName,
        @RequestParam("pid") Long pid, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("pid", String.valueOf(pid));
        kv.add("contentName", contentName);
        String result = rentApi.postMultiValueMap(rentApi.getApiBaseURL()
            + RentApi.INSERT_CONTENT, kv, settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "创建成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "deleteContent", method = RequestMethod.POST)
    public Map<String, Object> deleteContent(@RequestParam("id") Long id,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        String result = rentApi.postMultiValueMap(rentApi.getApiBaseURL()
            + RentApi.DELETE_CONTENT, kv, settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "删除成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "selectContentByPid", method = RequestMethod.GET)
    public List<Content> selectContentByPid(@RequestParam("pid") Long pid,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL())
            .append(RentApi.SELECT_CONTENT_BY_PID)
            .append("?appkey={appkey}");
        if (pid != null) {
            kv.put("pid", String.valueOf(pid));
            builder.append("&pid={pid}");
        }
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONArray data = jo.getJSONArray("data");
                Gson gson = new GsonBuilder().create();
                List<Content> contents = gson.fromJson(data.toString(),
                    new TypeToken<List<Content>>() {
                    }.getType());
                return contents;
            }
        }
        return null;
    }

    @RequestMapping(value = "updateProperty", method = RequestMethod.POST)
    public Map<String, Object> updateProperty(
        @RequestParam("id") Long id,
        @RequestParam(required = false, value = "viewName") String viewName,
        HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");

        Map<String, Object> map = new HashMap<String, Object>();
        MultiValueMap<String, String> kv = new LinkedMultiValueMap<String, String>();
        kv.add("id", String.valueOf(id));
        kv.add("viewName", viewName);
        String result = rentApi.postMultiValueMap(rentApi.getApiBaseURL()
            + RentApi.UPDATE_PROPERTY, kv, settingUtils.get());
        JSONObject jo = new JSONObject(result);
        Integer code = jo.getInt("code");
        if (code == 0) {
            map.put("message", "修改成功");
            map.put("success", true);
        } else {
            map.put("message", jo.getString("message"));
            map.put("success", false);
        }
        return map;
    }

    @RequestMapping(value = "selectAllProperty", method = RequestMethod.GET)
    public List<Property> selectAllProperty(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Map<String, String> kv = new HashMap<String, String>();
        StringBuilder builder = new StringBuilder();
        builder.append(rentApi.getApiBaseURL())
            .append(RentApi.SELECT_ALL_PROPERTY).append("?appkey={appkey}");
        String result = rentApi.get(builder.toString(), kv, settingUtils.get());
        if (null != result) {
            JSONObject jo = new JSONObject(result);
            Integer code = jo.getInt("code");
            if (code == 0) {
                JSONArray data = jo.getJSONArray("data");
                Gson gson = new GsonBuilder().create();
                List<Property> propertys = gson.fromJson(data.toString(),
                    new TypeToken<List<Property>>() {
                    }.getType());
                return propertys;
            }
        }
        return null;
    }

}
